package org.csu.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.mypetstore.commonResponse.CommonResponse;
import org.csu.mypetstore.domin.Category;
import org.csu.mypetstore.domin.Product;
import org.csu.mypetstore.persistence.ProductMapper;
import org.csu.mypetstore.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("ProductService")

public class ProductServiceImpl implements ProductService {

   @Autowired
    private ProductMapper productMapper;

    @Override
    public CommonResponse<List<Product>> getProductListByCategory(String categoryId) {
        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("category",categoryId);

        List<Product> productList=productMapper.selectList(queryWrapper);
        if(categoryId==null){
            return CommonResponse.createForError("通过categoryId无法查找商品信息");
        }
        return CommonResponse.createForSuccess("查找成功",productList);
    }


    @Override
    public CommonResponse<Product> getProduct(String productId) {
        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("productid",productId);

        Product product=productMapper.selectOne(queryWrapper);
        if(productId==null){
            return CommonResponse.createForError("通过productId无法查找商品信息");
        }
        return CommonResponse.createForSuccess("查找成功",product);
    }

    @Override
    public CommonResponse<List<Product>> searchProductList(String keywords) {
        QueryWrapper<Product>queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name",keywords);

        List<Product>productList= productMapper.selectList(queryWrapper);
        return CommonResponse.createForSuccess(productList);
    }

    @Override
    public List<Product> getAllProduct() {
       return productMapper.selectList(null);
    }
}
